package com.tucy.logistics.modules.vehicle.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tucy.logistics.common.dto.vehicle.QueryVehicleDto;
import com.tucy.logistics.common.entity.user.SystemFile;
import com.tucy.logistics.common.entity.vehicle.VehicleInfo;
import com.tucy.logistics.common.entity.vehicle.VehicleLicense;
import com.tucy.logistics.modules.assist.file.service.SystemFileService;
import com.tucy.logistics.modules.vehicle.service.VehicleInfoService;
import com.tucy.logistics.modules.vehicle.mapper.VehicleInfoMapper;
import com.tucy.logistics.modules.vehicle.service.VehicleLicenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author tucy123
* @description 针对表【vehicle_info(车辆信息表)】的数据库操作Service实现
* @createDate 2023-10-12 15:28:54
*/
@Service
public class VehicleInfoServiceImpl extends ServiceImpl<VehicleInfoMapper, VehicleInfo> implements VehicleInfoService{

    @Autowired
    VehicleInfoMapper vehicleInfoMapper;

    @Autowired
    VehicleLicenseService vehicleLicenseService;

    @Autowired
    SystemFileService systemFileService;

    @Override
    public Page<VehicleInfo> queryVehicleInfo(QueryVehicleDto queryVehicleDto) {
        Page<VehicleInfo> page = new Page<>(queryVehicleDto.getPageNum(),queryVehicleDto.getPageSize());

        QueryWrapper<VehicleInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .like(ObjectUtil.isNotEmpty(queryVehicleDto.getVehicleNumber()),VehicleInfo::getVehicleNumber,queryVehicleDto.getVehicleNumber())
                .eq(ObjectUtil.isNotEmpty(queryVehicleDto.getVehicleTypeId()),VehicleInfo::getVehicleTypeId,queryVehicleDto.getVehicleTypeId())
                .like(ObjectUtil.isNotEmpty(queryVehicleDto.getVehicleCode()),VehicleInfo::getVehicleCode,queryVehicleDto.getVehicleCode())
                .like(ObjectUtil.isNotEmpty(queryVehicleDto.getVehicleTypeName()),VehicleInfo::getVehicleLogo,queryVehicleDto.getVehicleLogo());

        return vehicleInfoMapper.selectPage(page,wrapper);
    }

    /**
     * 车辆详情
     * @param id
     * @return
     */
    @Override
    public VehicleInfo queryVehicleInfoDetails(String id) {

        VehicleInfo vehicleInfo = vehicleInfoMapper.selectById(id);

        VehicleLicense vehicleLicense = vehicleLicenseService.query().eq("vehicle_number", vehicleInfo.getVehicleNumber()).one();

        List<SystemFile> files = systemFileService.query().eq("connect_id", vehicleInfo.getId()).list();
        if (ObjectUtil.isNotEmpty(files)) {
            vehicleInfo.setVehicleFiles(files);
        }

        List<SystemFile> licenseFiles = systemFileService.query().eq("connect_id", vehicleLicense.getId()).list();
        if (ObjectUtil.isNotEmpty(licenseFiles)) {
            vehicleLicense.setLicenseFiles(licenseFiles);
        }

        vehicleInfo.setLicense(vehicleLicense);

        return vehicleInfo;
    }

    /**
     * 增改车辆
     * @param vehicleInfo
     * @return
     */
    @Override
    public Boolean saveUpdateVehicleInfo(VehicleInfo vehicleInfo) {

        boolean saveOrUpdate = this.saveOrUpdate(vehicleInfo);

        List<SystemFile> files = vehicleInfo.getVehicleFiles();
        if (ObjectUtil.isNotEmpty(files)) {
            files.forEach(file -> file.setConnectId(vehicleInfo.getId()));
            systemFileService.saveOrUpdateBatch(files);
        }

        VehicleLicense license = vehicleInfo.getLicense();
        vehicleLicenseService.saveOrUpdate(license);

        List<SystemFile> licenseFile = license.getLicenseFiles();
        if (ObjectUtil.isNotEmpty(licenseFile)) {
            licenseFile.forEach(file -> file.setConnectId(license.getId()));
            systemFileService.saveOrUpdateBatch(licenseFile);
        }

        return saveOrUpdate;
    }
}




